#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<TreeNode*> allPossibleFBT(int n)
    {
        if (n % 2 == 0)
        {
            return std::vector<TreeNode*>();
        }

        return bulidFBT(1, n);
    }

private:
    std::vector<TreeNode*> bulidFBT(int down, int up)
    {
        if (down == up)
        {
            TreeNode* node = new TreeNode(0);
            return std::vector<TreeNode*>(1, node);
        }

        std::vector<TreeNode*> tree;

        for (int root = down + 1; root < up; root += 2)
        {
            std::vector<TreeNode*> lefts = bulidFBT(down, root - 1);
            std::vector<TreeNode*> rights = bulidFBT(root + 1, up);

            for (TreeNode* left : lefts)
            {
                for (TreeNode* right : rights)
                {
                    TreeNode* node = new TreeNode(0);
                    node->left = left;
                    node->right = right;
                    tree.push_back(node);
                }
            }
        }

        return tree;
    }
};